const fs = require('fs')
// const xlsx = require('js-xlsx')
// let filePath = 'D:/Mycode/international_airport/doc/音频/音频文档.xlsx';
// let buf = fs.readFileSync(filePath);
// let workbook = xlsx.readFile(filePath, {
//     type: 'utf-8',
// })
// console.log('workbook', workbook)
// let data = {};
// let sheet = workbook.Sheets.Sheet1
// let keys = Object.keys(sheet).filter(v => {
//     return v && v.match(/^A\d+/)
// });
// for (let key of keys) {
//     let val = sheet[key].w;
//     if (key == 'A1' || !val)
//         continue;
//     console.log('val', val)
//     let dataKey = val.split(' ').filter(v => { return !!v }).join('_')
//     data[dataKey] = val;
// }
// fs.writeFileSync('exportData.json', Buffer.from(keys.toString()))
// fs.writeFileSync('exportAudioList.json', Buffer.from(JSON.stringify(data)))
function writeFile(url, strData, callback) {
    if (!fs.existsSync(Editor.url(url))) {
        Editor.assetdb.create(url, strData, callback)
    } else {
        Editor.assetdb.saveExists(url, strData, callback)
    }
}
module.exports = {
    start: function () {

        let url = Editor.url('db://assets/resources/sounds/') + '/'
        let languageUrl = url + 'language/zh/';
        Editor.log('sayHello', languageUrl)
        let list = fs.readdirSync(languageUrl);
        let data = {};
        let DTSData = 'declare const audioList:{'
        for (let v of list) {
            let val = v;
            if (val.match(/.meta$/)) {
                continue;
            }
            Editor.log('v', v)

            val = val.replace(/.mp3$/, '');

            let fileUrl = languageUrl + v;
            Editor.log('fileUrl', fileUrl)
            if (fs.statSync(fileUrl).isFile()) {
                let key = val;
                key = key.replace(/ /g, '_');
                key = key.replace(/-/g, '_');
                key = key.replace(/\'/g, '');
                key = key.replace(/\(/g, '');
                key = key.replace(/\)/g, '');
                key = key.replace(/\#/g, 'X');
                key = key.replace(/\,/g, '_');
                key = key.replace(/\./g, '_');
                if (key[0].match(/\d/)) {
                    key = '_' + key;
                }
                data[key] = val;
                DTSData += `\r\n\t${key}:string,`
            }
        }
        DTSData += '}';

        Editor.log('data', data)
        let exportUrl = 'db://assets/exportAudioList.ts';
        let strData =
            `
const audioList = ${Buffer.from(JSON.stringify(data))}
window['audioList'] = audioList;
`
        writeFile(exportUrl, strData, (e) => {
            Editor.log('导出成功', e);
        })
        writeFile('db://assets/exportAudioList.d.ts', DTSData, (e) => {
            Editor.log('导出dts成功', e);
        })
        // fs.writeFileSync(url + 'exportAudioList.json', Buffer.from(JSON.stringify(data)))
    }
}
